A = AjaxStack.ArrayExt;

YAHOO.namespace("ajaxstack");
YAHOO.ajaxstack.ArrayExt = new YAHOO.tool.TestCase({

	name: "ArrayExt Tests",

	//--------------------------------------------- 
	// Setup and tear down 
	//---------------------------------------------

	setUp: function()
	{
	},

	tearDown: function()
	{
	},

	//--------------------------------------------- 
	// Tests 
	//---------------------------------------------

	testContainsValue: function()
	{
		Assert.isTrue(A.containsValue(['a'], 'a'));
		Assert.isTrue(A.containsValue(['a', 'b', 'c'], 'b'));
		Assert.isFalse(A.containsValue(['a', 'b', 'c'], 'A'));
		Assert.isFalse(A.containsValue(['a', 'b', 'c'], ''));
	},

	testAny: function()
	{
		Assert.isTrue(A.any([2, 4, 6], function(n) { return n % 2 == 0; }));
		Assert.isFalse(A.any([2, 4, 6], function(n) { return n % 2 == 1; }));
		Assert.isTrue(A.any([1, 2, 3, 5], function(n) { return n % 2 == 0; }));
		Assert.isFalse(A.any([1, 3, 5], function(n) { return n % 2 == 0; }));

		Assert.isTrue(A.any(["a", "ate", "apple"], function(s) { return S.startsWith(s, "a"); }));
		Assert.isTrue(A.any(["a", "b", "c"], function(s) { return s == "b"; }));
		Assert.isTrue(A.any(["a", "b", "c"], function(s) { return S.startsWith(s, "b"); }));
		Assert.isFalse(A.any(["a", "b", "c"], function(s) { return s == "d"; }));
	},

	testAll: function()
	{
		Assert.isTrue(A.all([2, 4, 6], function(n) { return n % 2 == 0; }));
		Assert.isFalse(A.all([2, 4, 6], function(n) { return n % 2 == 1; }));
		Assert.isFalse(A.all([1, 2, 3, 5], function(n) { return n % 2 == 0; }));
		Assert.isFalse(A.all([1, 3, 5], function(n) { return n % 2 == 0; }));

		Assert.isTrue(A.all(["a", "ate", "apple"], function(s) { return S.startsWith(s, "a"); }));
		Assert.isFalse(A.all(["a", "b", "c"], function(s) { return s == "b"; }));
		Assert.isFalse(A.all(["a", "b", "c"], function(s) { return S.startsWith(s, "b"); }));
		Assert.isFalse(A.all(["a", "b", "c"], function(s) { return s == "d"; }));
	},

	testRemove: function()
	{
		ArrayAssert.itemsAreEqual([4, 6, 8], A.remove([2, 4, 6, 8], 1));
		ArrayAssert.itemsAreEqual([4], A.remove([2, 4, 6], 1, 2));
		ArrayAssert.itemsAreEqual([4, 6], A.remove([2, 4, 6], 1, 3));

		var arr = [2, 4, 6, 8, 10];
		var arr2 = A.remove(arr, 1, 2);
		ArrayAssert.itemsAreEqual([2, 8, 10], arr);
	},

	testJoin: function()
	{
		Assert.areEqual("1,2,3", A.join([1, 2, 3]));
		Assert.areEqual("1|2|3", A.join([1, 2, 3], "|"));
		Assert.areEqual("a|b|c", A.join(['a', 'b', 'c'], "|"));
	},

	testIsEmpty: function()
	{
		Assert.isTrue(A.isEmpty(null));
		Assert.isTrue(A.isEmpty([]));
		Assert.isFalse(A.isEmpty([1]));
		Assert.isFalse(A.isEmpty(['a']));
	},

	testEach: function()
	{
		var sum = 0;
		A.each([1, 2, 3], function(n) { sum += n; });
		Assert.areEqual(1 + 2 + 3, sum);

		var str = "";
		A.each(['a', 'b', 'c'], function(s) { str += s; });
		Assert.areEqual("abc", str);
	},

	testMerge: function()
	{
		var arr1 = [1, 2], arr2 = [3, 4];
		A.merge(arr1, arr2);
		ArrayAssert.itemsAreEqual([1, 2, 3, 4], arr1);

		var sArr1 = ['a', 'b'], sArr2 = ['c', 'd'];
		A.merge(sArr1, sArr2);
		ArrayAssert.itemsAreEqual(['a', 'b', 'c', 'd'], sArr1);
	},

	testClone: function()
	{
		var arr1 = [1, 2, 3];
		var arr2 = A.clone(arr1);
		ArrayAssert.itemsAreEqual([1, 2, 3], arr1);
		ArrayAssert.itemsAreEqual([1, 2, 3], arr2);

		arr2.push(4);
		ArrayAssert.itemsAreEqual([1, 2, 3], arr1);
		ArrayAssert.itemsAreEqual([1, 2, 3, 4], arr2);
	}

});

